package com.wangnian.controller;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.base.Strings;



@Controller
@RequestMapping("/tools")
public class Tools {
   
	/**
	 * 跳转到获取响应头的页面
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/doResponseHead" )
	public String  doGetResponseHead(String strurl,Integer count,HttpServletRequest request,Model model) throws IOException
	{
	  if(Strings.isNullOrEmpty(strurl))
	  {
		  model.addAttribute("isdisplay",false);
		  return "responseHead"; 
	  }
	  List li=new ArrayList();
	  URL url = new URL(strurl);
	  for(int i=0;i<count;i++)
	  {
		URLConnection conn = url.openConnection();
		Map headers = conn.getHeaderFields();
		Set<String> keys = headers.keySet();
		for( String key : keys )
		{
			String val = conn.getHeaderField(key);
			if(key!=null && key.equalsIgnoreCase("Server"))
			{
				 li.add(key+"    "+val);	
			}
	    }
		
	  }
	  List serlist=new ArrayList();
	  HashSet h=new  HashSet(li); 
	  serlist.addAll(h); 
	  model.addAttribute("list", li);
	  model.addAttribute("url", strurl);
	  model.addAttribute("count", count);
	  model.addAttribute("serlist", serlist);
	  model.addAttribute("isdisplay",true);
	  return "responseHead";
  }
	
	
}
